//
//  ThirdPartLoginViewController.m
//  Growth
//
//  Created by Hobo.Chen on 4/13/12.
//  Copyright (c) 2012 MEETStudio. All rights reserved.
//

#import "ThirdPartLoginViewController.h"

#define SMOOTHGREEN [UIColor colorWithRed:138.0/255.0 green:177.0/255.0 blue:42.0/255.0 alpha:1.0];
#define FONT [UIFont systemFontOfSize:14]

static UITextField *activeField;

static WebConnectionArray *verifyEmail_connetion;
static WebConnectionArray *verifyNicName_connetion;

@implementation ThirdPartLoginViewController


@synthesize emailTextField;
@synthesize  keyTextField;
@synthesize nicknameTextField;

@synthesize delegate;
@synthesize userInfo_dict;

@synthesize myScrollView;
@synthesize _platformId;


-(id)initWithUserInfo:(NSDictionary *)userInfoDict platformId:(int)platformId{
    
    [super init];
    
    self.userInfo_dict = [userInfoDict copy];
    self._platformId = platformId;
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


#pragma mark - textField delegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField.tag == 1) {
        [keyTextField becomeFirstResponder];
    }
    else{
        [textField resignFirstResponder];
    }
    return YES;
}


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    activeField = textField;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    activeField = nil;
}
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"帐号已有绑定";
    
    //定义返回按钮
    UIBarButtonItem * backBarButtonItem=[[UIBarButtonItem alloc] initWithTitle: @"返回" style:UIBarButtonSystemItemDone target:self action: nil];   
    
    [[self navigationItem] setBackBarButtonItem:backBarButtonItem];  
    [backBarButtonItem release]; 

    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}



#pragma mark 跳过绑定  
-(IBAction)derectLogin:(id)sender{
    
             
    ThirdPartNickNameView *viewController = [[ThirdPartNickNameView alloc]initWithUserInfo:userInfo_dict platformId:_platformId];
    [self.navigationController pushViewController:viewController animated:YES];
        
    [viewController release];
}

#pragma mark 绑定已有账号

-(IBAction)bindLogin:(id)sender{
     
    //先验证帐号是否存在，再绑定
    [self verifyEmail];
    
    
  //  [self.delegate bindQQUser:emailTextField.text key:keyTextField.text];
    
}

-(void)verifyEmail{
    NSMutableURLRequest * request = [WebRequest verifyEmailRequest:emailTextField.text];
    
    CLog(@"request %@", request);
    if (verifyEmail_connetion != nil) {
        verifyEmail_connetion.connectionDelegate = nil;
    }
    verifyEmail_connetion = [[WebConnectionArray alloc] initWith:request delegate:self dataBaseListName:nil];
    
}


-(void)verifyNickname{
    
    NSMutableURLRequest * request = [WebRequest verifyNickNameRequest:nicknameTextField.text];
    
    CLog(@"request %@", request);
    if (verifyNicName_connetion != nil) {
        verifyNicName_connetion.connectionDelegate = nil;
    }
   verifyNicName_connetion = [[WebConnectionArray alloc] initWith:request delegate:self dataBaseListName:nil];
    
}

#pragma mark -WebConnection delegate

- (void)connectionArraySuccess:(NSDictionary *)dict connectionURL:(NSString *)url{
    
    CLog(@"info %@",  dict);
       [LoadingView hideWithAnimated:YES];
    
    if (dict==nil) {
        return;
    }
    
    
    //验证账号
         
       
    
	if (![[dict objectForKey:@"object"] boolValue]) {//该账户已注册
        
         [self.delegate bindQQUser:emailTextField.text key:keyTextField.text];
    }  
    else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"该帐户不存在！" 
                                                       delegate:self 
                                              cancelButtonTitle:@"确定" otherButtonTitles:nil];
        
        [alert show];
        [alert release];

    }
    
}

- (void)connectionArrayError:(NSError *)error{
    [LoadingView showImage:[UIImage imageNamed:@"wangluo_iphone.png"] info:@"网络请求错误或超时！" duration:DEFAULT_LOADING_TIME *1.2];      
}


-(void)dealloc{
    
    if (verifyEmail_connetion != nil) {
        verifyEmail_connetion.connectionDelegate = nil;
        [verifyEmail_connetion release];
    }
    if (verifyNicName_connetion != nil) {
        verifyNicName_connetion.connectionDelegate = nil;
        [verifyNicName_connetion release];
    }
    
    [myScrollView release];
    [emailTextField release];
    [keyTextField release];
    [nicknameTextField release];
    [userInfo_dict release];
    [super dealloc];
    
}



#pragma mark keyboard
- (void) registerForKeyboardNotifications{
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardDidShowNotification object:nil];
    
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardWillHideNotification object:nil];
    
}


- (void) keyboardWasShown:(NSNotification *) notif{
    NSDictionary *info = [notif userInfo];
    
    NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [value CGRectValue].size;
    
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    myScrollView.contentInset = contentInsets;
    myScrollView.scrollIndicatorInsets = contentInsets;
    
    // If active text field is hidden by keyboard, scroll it so it's visible
    // Your application might not need or want this behavior.
    CGRect aRect = self.contentView.frame;
    aRect.size.height -= keyboardSize.height;
    if (!CGRectContainsPoint(aRect, activeField.frame.origin) ) {
        //CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-keyboardSize.height);
        CGPoint scrollPoint = CGPointMake(0.0, keyboardSize.height);
        [myScrollView setContentOffset:scrollPoint animated:YES];
    }
}

- (void) keyboardWasHidden:(NSNotification *) notif{
  //  NSDictionary *info = [notif userInfo];
    
   // NSValue *value = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    //CGSize keyboardSize = [value CGRectValue].size;
    
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    myScrollView.contentInset = contentInsets;
    myScrollView.scrollIndicatorInsets = contentInsets;    
}



@end
